<template>
  <el-row :gutter="20" style="padding: 20px">
    <el-col :span="12">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
        <el-form-item label="新手机号" prop="phone">
          <el-input v-model="ruleForm.phone" placeholder="请输入新手机号"></el-input>
        </el-form-item>
        <el-form-item prop="smsCaptchaResult" label="短信验证码">
          <el-input style="width: 180px; margin-right: 10px" prefix-icon="el-icon-chat-dot-square" v-model="ruleForm.smsCaptchaResult" placeholder="短信验证码"></el-input>
          <el-button type="primary" v-if="!countDown" @click="sendVerifyCode">发送</el-button>
          <el-button type="primary" :disabled="true" v-else>{{ countDown }}s</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>
  <script>
import router from "@/router";
export default {
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!/^1[3-9]\d{9}$/.test(value)) {
        callback("请输入正确的手机号码");
      }
      callback();
    };
    return {
      isSuccess: false,
      uuid: "",
      smsUuid: "",
      codeUrl: "",
      countDown: 0, // 倒计时
      ruleForm: {
        smsCaptchaResult: "",
        phone: "",
      },
      rules: {
        phone: [
          { required: true, message: "请输入新手机号", trigger: "blur" },
          { validator: validateUsername },
        ],
        smsCaptchaResult: [
          {
            required: true,
            message: "请输入手机验证码",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    onSuccess() {
      this.isSuccess = true;
    },
    // 4.获取手机验证码
    async sendVerifyCode() {
      const params = {
        phone: this.ruleForm.phone,
        uuid: this.uuid,
      };
      // 4.2 获取短信验证码
      this.$post("/customer/NewUserNameSms", params)
        .then((res) => {
          if (res && res.code * 1 === 200) {
            this.smsUuid = res.data;
            this.$message.success("短信发送成功");
            this.countDown = 60;
            this.timeIntervalID = setInterval(() => {
              this.countDown--;
              // 4.1 如果减到0 则清除定时器
              if (this.countDown == 0) {
                clearInterval(this.timeIntervalID);
              }
            }, 1000);
          } else {
            //
          }
        })
        .catch(() => {});
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const params = {
            phone: this.ruleForm.phone,
            code: this.ruleForm.smsCaptchaResult,
            uuid: this.smsUuid,
          };
          this.$post("/customer/updateUserName", params)
            .then((res) => {
              if (res && res.code * 1 === 200) {
                this.$message.success("操作成功");
                this.ruleForm = {
                  smsCaptchaResult: "",
                  phone: "",
                };
                localStorage.removeItem("token");
                localStorage.removeItem("userInfo");
                router.push("/homePage");
              }
            })
            .finally(() => {
              clearInterval(this.timeIntervalID);
              this.countDown = 0;
            });
        } else {
          return false;
        }
      });
    },
  },
  mounted() {},
};
</script>
<style scoped lang="less">
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.login-code-img {
  height: 38px;
}
</style>